package cn.marinda.gui.impl;

import cn.marinda.gui.AbstractHelper;
import cn.marinda.listener.*;
import lombok.SneakyThrows;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务器控制台助手impl
 *
 * @author admin
 * @date 2022/07/08
 */
public class ServerConsoleHelperImpl extends AbstractHelper {

    public static ServerConsoleHelperImpl instance = new ServerConsoleHelperImpl();
    private JTextArea serverTextArea;
    /**
     * 服务器控制台助手impl
     */
    private ServerConsoleHelperImpl(){
        frame.setVisible(false);
    }


    /**
     * 单击服务器控制按钮
     *
     * @param text 文本
     */
    private void bindButtonActionListener(String text, JButton button){
        ActionListener actionListener = null;
        switch (text){
            case "发送命令":
                button.setEnabled(true);
                actionListener = new SendCommandServerActionEvent(this);
                break;
            case "开启服务器":
                button.setEnabled(true);
                actionListener = new StartServerActionEvent(this);
                break;
            case "停止服务器":
                button.setEnabled(false);
                actionListener = new StopServerActionEvent(this);
                break;

            case "重启服务器":
                button.setEnabled(false);
                actionListener = new ResetServerActionEvent(this);
                break;
        }

        button.addActionListener(actionListener);
    }


    @Override
    protected JFrame loadFrame() {
        frame = new JFrame();
        frame.setSize(750,600);
        frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setTitle("服务端");
        frame.setJMenuBar(menuBar);
        frame.setContentPane(boxPanel);
        frame.setResizable(false);
        return frame;
    }

    @Override
    protected JPanel loadPanel() {
        Box horizontalBox = Box.createHorizontalBox();
        Box horzontal2Box = Box.createHorizontalBox();
        Box centerBox = Box.createHorizontalBox();
        Box cmdBox = Box.createHorizontalBox();
        JScrollPane scrollPane;
        JButton cmdBtn = null;
        for(Component component : componentList){
            if(component instanceof JTextArea){
                JTextArea area = (JTextArea) component;
                area.setFont(new Font(null,Font.PLAIN,15));
                area.setEnabled(false);
                area.setDragEnabled(false);
                serverTextArea = area;
                horizontalBox.add(Box.createHorizontalStrut(30));
                scrollPane = new JScrollPane(area);
                horizontalBox.add(scrollPane);
                horizontalBox.add(Box.createHorizontalStrut(30));
            }
            if(component instanceof JButton){
                JButton btn = (JButton) component;
                if(btn.getText().equalsIgnoreCase("发送命令")){
                    cmdBtn = (JButton) component;
                    bindButtonActionListener(cmdBtn.getText(),cmdBtn);
                    continue;
                }
//                给每个相关按钮绑定上点击事件
                bindButtonActionListener(btn.getText(),btn);
                horzontal2Box.add(Box.createHorizontalStrut(30));
                horzontal2Box.add(btn);
                horzontal2Box.add(Box.createHorizontalStrut(20));
            }
            if(component instanceof JLabel){
                JLabel label = (JLabel) component;
                centerBox.add(Box.createHorizontalStrut(30));
                centerBox.add(label);
                centerBox.add(Box.createHorizontalStrut(30));
            }
            if(component instanceof JTextField){
                JTextField field = (JTextField) component;
                cmdBox.add(Box.createHorizontalStrut(30));
                field.setMargin(new Insets(0,10,0,0));
                field.addKeyListener(new SendCommandKeyEvent(field,serverTextArea));
                cmdBox.add(field);
                cmdBox.add(Box.createHorizontalStrut(30));
                cmdBox.add(cmdBtn);
                cmdBox.add(Box.createHorizontalStrut(30));
            }
        }
        boxPanel.add(Box.createVerticalStrut(30));
        boxPanel.add(horizontalBox);
        boxPanel.add(Box.createVerticalStrut(30));
        boxPanel.add(centerBox);
        boxPanel.add(Box.createVerticalStrut(30));
        boxPanel.add(cmdBox);
        boxPanel.add(Box.createVerticalStrut(50));
        boxPanel.add(horzontal2Box);
        boxPanel.add(Box.createVerticalStrut(50));
        return view;
    }


    @Override
    protected List<Component> loadComponent() {
        JTextArea serverConsoleTextArea = createTextAreaComponent(10,40);
        JLabel createServer = createTextComponent("没有服务端？菜单栏选择创建服务端！");
        JButton startServer = createButtonComponent("开启服务器",Color.BLACK);
        JButton stopServer = createButtonComponent("停止服务器",Color.BLACK);
        JButton resetServer = createButtonComponent("重启服务器",Color.BLACK);
        JTextField command = createTextFieldComponent(2);
        JButton sendCmdButton = createButtonComponent("发送命令",Color.BLACK);
        componentList.add(serverConsoleTextArea);
        componentList.add(createServer);
        componentList.add(startServer);
        componentList.add(stopServer);
        componentList.add(resetServer);
        componentList.add(sendCmdButton);
        componentList.add(command);

        return componentList;
    }
}
